﻿<Cmdlet(VerbsLifecycle.Invoke, "SqlScalar")>
Public Class InvokeSqlScalar
    Inherits PSCmdlet

    <Parameter(Mandatory:=True, Position:=1)>
    Public Property Query As String
    <Parameter(Position:=2)>
    Public Property Parameters As Hashtable
    <Parameter>
    Public Property ConnectionName As String = "Default"
    <Parameter>
    Public Property CommandTimeout As Integer = -1

    Protected Overrides Sub EndProcessing()
        MyBase.EndProcessing()

        If Not DBInfo.Connections.ContainsKey(Me.ConnectionName) Then
            WriteWarning(DBInfo.NoConnection(Me.ConnectionName))
            Exit Sub
        End If

        DBInfo.ProcessHashTable(Me.Parameters)
        DBInfo.Connections(Me.ConnectionName).ClearMessages()

        Using cmd As IDbCommand = DBInfo.Connections(Me.ConnectionName).GetCommand(Query, CommandTimeout, Parameters)
            WriteObject(cmd.ExecuteScalar())
        End Using
    End Sub
End Class
